<?php

namespace app\api\controller;

use app\api\model\Collect;
use app\api\model\Order;
use app\api\model\Report;
use app\api\controller\Common;
use app\api\model\SearchLog;
use app\api\model\User as UserM;
use app\common\controller\Api;
use app\api\model\House AS HouseM;
class House extends Api
{
    protected $noNeedLogin = ['list','search','details'];
    protected $noNeedRight = ['*'];

    /**
     * 房源列表
     * @return void
     * @throws \think\exception\DbException
     */
    public function list(){
        $param = request()->param();
        $is_recommend = request()->param('is_recommend',0);
        $user=$this->auth;

        $HouseM = new HouseM();
        if(!empty($param['introduce'])){
            $where['introduce']=['like',"%".$param['introduce']."%"];
        }
        if(!empty($param['area'])){
            $where['area']=['like',"%".$param['area']."%"];
        }
        if(!empty($param['time_type'])){
            if($param['time_type']==1){
                $start=strtotime(date('Y-m-d 00:00:00',time()));
            }
            if($param['time_type']==2){
                $start=strtotime('-7 day', time());
            }
            if($param['time_type']==3){
                $start=strtotime('-30 day', time());
            }
            if($param['time_type']==4){
                $start=strtotime('-365 day', time());
            }
            $end=time();
            $where['createtime']=['between',[$start,$end]];

        }
//        dump($param);
        if(!empty($param['class_id'])){
            $where['class']=$param['class_id'];

        }
        $where['status']=['in',$param['status']];
        $where['is_pay']=1;
        $where['is_arrive']=1;
        $where['is_careful']=1;
        $where['is_del']=0;
        if(!empty($is_recommend)){
            $where['is_recommend']=$is_recommend;
        }
        $field="*";
        $list=$HouseM->where($where)->field($field)->order('id desc')
            ->paginate()->each(function ($item)use($user){
                $Collect = new Collect();
                $is_collect=0;
                if(!empty($user)){
                    $where_collect['user_id']=$user->id;
                    $where_collect['house_id']=$item['id'];
                    $collect=$Collect->where($where_collect)->find();
                    if(!empty($collect)){
                        $is_collect=1;
                    }
                }

                $item['is_collect']=$is_collect;
            return $item;
        });
        $this->success('',$list);
    }

    /**
     * 房源详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function details(){
        $param = request()->param();
        $user=$this->auth;
        $Collect = new Collect();
        $HouseM = new HouseM();
        $where['id']=$param['id'];
        $find=$HouseM->where($where)->find();
        $updata['browse_num']=$find['browse_num']+1;
        $HouseM->save($updata,$where);
        $is_collect=0;
        $where_collect['user_id']=$user->id;
        $where_collect['house_id']=$param['id'];
        $collect=$Collect->where($where_collect)->find();
        if(!empty($collect)){
            $is_collect=1;
        }
        $find['is_collect']=$is_collect;
        $this->success('',$find);
    }

    /**
     * 获取电话
     * @return void
     */
    public function getPhone()
    {
        $param = request()->param();
        $user=$this->auth;
        //先判断是否需要支付
        $is_pay=1;
        $is_get_phone=db('get_phone')->where(['user_id'=>$user->id,'h_id'=>$param['id']])->find();
        if(!empty($is_get_phone)){
            $is_pay=0;
        }
        $data['is_pay']=$is_pay;
        $data['get_phone_money']=config('site.get_phone');

        $this->success('',$data);
    }

    /**
     * 支付获取手机号费用
     * @return void
     */
    public function payPhone()
    {
        $param = request()->param();
        $user=$this->auth;

        $HouseM = new HouseM();

        $money=config('site.get_phone');

        if($money>$user->money){
            $this->error('钢豆不足');
        }
        if($money>0) {
            $this->user_money($user->id, $money, 0, "获取手机号花费钢豆");
        }
        $data['user_id']=$user->id;
        $data['h_id']=$param['id'];
        $data['pay_time']=time();
        $data['money']=$money;
        db('get_phone')->insert($data);

        $Common= new Common();
        $find=$HouseM->where(['id'=>$param['id']])->find();
        $Common->Alibaba_Sms($find['phone'],'SMS_475740048');

        $this->success('');
    }
    public function edit(){
        $param = request()->param();
        $HouseM = new HouseM();
        $where['id']=$param['id'];
        $param['is_careful']=0;
        $updata=$param;
        $HouseM->save($updata,$where);
        $this->success('成功');
    }
    public function del(){
        $param = request()->param();
        $HouseM = new HouseM();
        $where['id']=$param['id'];
        $param['is_del']=1;
        $updata=$param;
        $HouseM->save($updata,$where);
        $this->success('成功');
    }
    /**
     * 发布 二手房/租房
     * @return void
     */
    public function add(){
        $param = request()->param();
        $user=$this->auth;
        $HouseM = new HouseM();
        $HouseM->user_id=$user->id;
        $HouseM->status=$param['status'];
        $HouseM->area=$param['area'];
        $HouseM->address=$param['address'];
        $HouseM->longitude=$param['longitude'];
        $HouseM->latitude=$param['latitude'];
        $HouseM->class=$param['class'];
        $HouseM->phone=$param['phone'];
        $HouseM->introduce=$param['introduce'];
        if(!empty($param['detailed_address'])){
            $HouseM->detailed_address=$param['detailed_address'];
        }

        if(!empty($param['group_images'])){
            $HouseM->group_images=$param['group_images'];
        }

        $HouseM->save();
        if($param['status']==1){//商品
            $data['ad_price']=$this->config_data('ad_goods');
        }elseif($param['status']==2){//需求
            $data['ad_price']=$this->config_data('ad_demand');
            $UserM = new UserM();
            $datas['is_unread']=1;
            $UserM->where(['is_unread'=>0])->update($datas);
        }else{
            $data['ad_price']=$this->config_data('ad_help');
        }
        $data['money']=$data['ad_price'];
        $data['id']=$HouseM->id;
        $this->success('',$data);
    }
    public function is_unread()
    {
        $user=$this->auth;
        $datas['is_unread']=0;
        $UserM = new UserM();
        $UserM->where(['id'=>$user->id])->update($datas);
        $this->success('');
    }

    public function pay()
    {
        $param = request()->param();
        $user=$this->auth;
        $HouseM = new HouseM();
        $find=$HouseM->where(['id'=>$param['id']])->find();
        if(empty($find)){
            $this->error('错误');
        }
//        dump($find['status']);die;
        if($find['status']==1){//商品
            $find['money']=$this->config_data('ad_goods');
        }if($find['status']==2){//需求
            $find['money']=$this->config_data('ad_demand');
        }else{
            $find['money']=$this->config_data('ad_help');
        }


        if($find['money']>$user->money){
            $this->error('钢豆不足');
        }
        if($find['money']>0){
            $this->user_money($user->id,$find['money'],0,"发布信息花费钢豆");
        }
        $where['id']=$param['id'];
        $updata['is_pay']=1;
        $HouseM->save($updata,$where);

        //开始给短信和公众号消息
        if($find['status']==3){
//            die("asd");
            $Common= new Common();
            $rrr=$Common->Alibaba_Sms($this->config_data('accept_phone'),'SMS_475875994');
//            dump($rrr);die;
        }

        $this->success('');
    }

    /**
     * 提交举报
     * @return void
     */
    public function report(){
        $param = request()->param();
        $user=$this->auth;
        $Report = new Report();
        $Report->user_id=$user->id;
        $Report->house_id=$param['house_id'];
        $Report->category_id=$param['category_id'];
        $Report->save();
        $this->success('提交成功');
    }

    /**
     * 历史搜索/热门搜索
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search(){
        $user=$this->auth;
        $SearchLog = new SearchLog();
        $where['user_id']=$user->id;
        $list=$SearchLog->where($where)->field('title')->order('id desc')->select();
        $data['history']=$list;
        $search=$this->config_data('search');
        $data['recommend']=explode(',',$search);
        $this->success('',$data);

    }
    public function search_del(){
        $user=$this->auth;
        $SearchLog = new SearchLog();
        $where['user_id']=$user->id;
        $SearchLog->where($where)->delete();
        $this->success('成功');

    }
    public function search_post(){
        $param = request()->param();
        $user=$this->auth;
        $SearchLog = new SearchLog();
        $where['user_id']=$user->id;
        $where['title']=$param['title'];
        $find=$SearchLog->where($where)->find();
        if(!empty($find)){
            $SearchLog->where($where)->delete();
        }
        $SearchLog->user_id=$user->id;
        $SearchLog->title=$param['title'];
        $SearchLog->save();

        $this->success('');
    }
    /**
     * 收藏/取消收藏
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collect(){
        $param = request()->param();
        $user=$this->auth;
        $Collect = new Collect();
        $where['user_id']=$user->id;
        $where['house_id']=$param['house_id'];
        $find=$Collect->where($where)->find();
        if(empty($find)){
            $Collect->user_id=$user->id;
            $Collect->house_id=$param['house_id'];
            $Collect->type=$param['type'];
            $Collect->save();
        }else{
            $Collect->where($where)->delete();
        }
        $this->success('成功');
    }

}
